home *** CD-ROM | disk | FTP | other *** search
- Path: user2.mnsinc.com!huang
- From: huang@mnsinc.com (Szu-Wen Huang)
- Newsgroups: comp.lang.c
- Subject: Re: [Q] Best handling of non-fixed-length data??
- Date: 18 Apr 1996 19:45:59 GMT
- Organization: Monumental Network Systems
- Message-ID: <4l665n$9uq@news1.mnsinc.com>
- References: <4l629f$4cc@nuke.csu.net>
- NNTP-Posting-Host: user2.mnsinc.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Eric Brower (etb1@Axe.Humboldt.edu) wrote:
-
- : Here is my application: I would like to store exam questions, along with
- : the correct answer and as many other possible answers per question, in a
- : file. From this file, I would like to be able to extract the question
- : and all possible answers through the question number I assign each question
- : within the database. My problem lies in memory management, as well as
- : method of storing this data. You see, I'd like questions to be as long
- : as need be, without using hideous amounts of space for the short questions
- : as well. As well, some questions may have only two possible answers, while
- : other may have five or more. I also must be able to extract the correct
- : answer from among the possibles.
-
- Here is my suggestion. Let me try to first jot down the assumptions:
-
- 1. any number of items
- 2. 1 question per item
- 3. 2 or more answers per question
- 4. 1 correct answer per question
-
- Okay, starting from the top going down, requirement #1 clearly indicates
- the need for a dynamic data structure. Since your question database is
- next to useless if you only have 10 or 20 questions, a linked-list might
- not be such a good idea. I'd suggest a binary tree.
-
- Requirement #2 is easy. A question is likely a string of characters.
- Since you don't want to waste space and the variance of the question
- lengths is large, the immediately obvious solution is to keep a char *
- instead of a char array.
-
- Requirement #3 is a bit tough. A linked-list of answers springs to
- mind, but if you know the maximum number of possible answers and it's
- fairly small, an array of pointers might work better. Based on the same
- variable-length string constraint as #2, this is an array of char *.
-
- Requirement #4 is certainly trivial. One answer is correct, so it's
- stored in a char. It's not likely that you'll have so many possible
- answers to need something more than a char - if you do, use an int.
-
- Okay, so our structure might look as follows:
-
- struct item {
- struct item *left_son, /* pointer to sons of the node, used */
- *right_son; /* to implement the binary tree */
- char *question;
- char *answers[MAX_POSSIBLE_ANSWERS];
- char correct_answer;
- };
-
- Ta-dah ;)
-